﻿using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using Sustainalytics.Entities;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.CorporateGovernance
{
    public interface IHashSetLoader
    {
        Task LoadAsync();
    }

    public class IndicatorsTemplateHashSetLoader : IHashSetLoader
    {
        private readonly IMongoCollection<GovernanceIndicatorTemplate> mongoCollection;

        private readonly IndicatorsHashSet indicatorCodesSet;

        public IndicatorsTemplateHashSetLoader(IMongoCollection<GovernanceIndicatorTemplate> mongoCollection, IndicatorsHashSet indicatorCodesSet)
        {
            this.mongoCollection = mongoCollection;
            this.indicatorCodesSet = indicatorCodesSet;
        }

        public async Task LoadAsync()
        {
            var values =
                await
                mongoCollection.Find(new BsonDocument()).Project(x => x.Number).ToListAsync().ConfigureAwait(false);

            foreach (var value in values)
            {
                if (!indicatorCodesSet.Contains(value))
                {
                    indicatorCodesSet.Add(value);
                }
            }
        }
    }
}
